home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / mysql-4.0.22-win / data1.cab / Development / scripts / mysql_convert_table_format < prev    next >
Encoding:
Text File  |  2004-10-28  |  2.9 KB  |  133 lines

  1. #!/usr/bin/perl
  2.  
  3. # Convert given tables in a database to MYISAM
  4.  
  5. use DBI;
  6. use Getopt::Long;
  7.  
  8. $opt_help=$opt_version=$opt_verbose=$opt_force=0;
  9. $opt_user=$opt_database=$opt_password=undef;
  10. $opt_host="localhost";
  11. $opt_socket="";
  12. $opt_type="MYISAM";
  13. $opt_port=0;
  14. $exit_status=0;
  15.  
  16. GetOptions("force","help","host=s","password=s","user=s","type=s","verbose","version","socket=s", "port=i") || 
  17.   usage(0);
  18. usage($opt_version) if ($#ARGV < 0 || $opt_help || $opt_version);
  19. $opt_database=shift(@ARGV);
  20.  
  21. if (uc($opt_type) eq "HEAP")
  22. {
  23.   print "Converting to type HEAP would delete your tables; aborting\n";
  24.   exit(1);
  25. }
  26.  
  27. $connect_opt="";
  28. if ($opt_port)
  29. {
  30.   $connect_opt.= ";port=$opt_port";
  31. }
  32. if (length($opt_socket))
  33. {
  34.   $connect_opt.=";mysql_socket=$opt_socket";
  35. }
  36.  
  37. $dbh = DBI->connect("DBI:mysql:$opt_database:${opt_host}$connect_opt",
  38.             $opt_user,
  39.             $opt_password,
  40.             { PrintError => 0})
  41.   || die "Can't connect to database $opt_database: $DBI::errstr\n";
  42.  
  43. if ($#ARGV < 0)
  44. {
  45.   # Fetch all table names from the database
  46.   my ($sth,$row);
  47.   $sth=$dbh->prepare("show tables");
  48.   $sth->execute || die "Can't get tables from $opt_database; $DBI::errstr\n";
  49.   while (($row = $sth->fetchrow_arrayref))
  50.   {
  51.     push(@ARGV,$row->[0]);
  52.   }
  53.   $sth->finish;
  54. }
  55.  
  56. print "Converting tables:\n" if ($opt_verbose);
  57. foreach $table (@ARGV)
  58. {
  59.   my ($sth,$row);
  60.  
  61.   # Check if table is already converted
  62.   $sth=$dbh->prepare("show table status like '$table'");  
  63.   if ($sth->execute && ($row = $sth->fetchrow_arrayref))
  64.   {
  65.     if (uc($row->[1]) eq uc($opt_type))
  66.     {
  67.       print "$table is already of type $opt_type;  Ignored\n";
  68.       next;
  69.     }
  70.   }
  71.   print "converting $table\n" if ($opt_verbose);
  72.   if (!$dbh->do("ALTER TABLE $table type=$opt_type"))
  73.   {
  74.     print STDERR "Can't convert $table: Error $DBI::errstr\n";
  75.     exit(1) if (!$opt_force);
  76.     $exit_status=1;
  77.   }
  78. }
  79.  
  80. $dbh->disconnect;
  81. exit($exit_status);
  82.  
  83.  
  84. sub usage
  85. {
  86.   my($version)=shift;
  87.   print "$0  version 1.1\n";
  88.   exit(0) if ($version);
  89.  
  90.   print <<EOF;
  91.  
  92. Conversion of a MySQL tables to other table types.
  93.  
  94.  Usage: $0 database [tables]
  95.  If no tables has been specifed, all tables in the database will be converted.
  96.  
  97.  The following options are available:
  98.  
  99. --force
  100.   Continue even if there is some error.
  101.  
  102. --help or --Information
  103.   Shows this help
  104.  
  105. --host='host name' (Default $opt_host)
  106.   Host name where the database server is located.
  107.  
  108. --password='password'
  109.   Password for the current user.
  110.  
  111. --port=port
  112.   TCP/IP port to connect to if host is not "localhost".
  113.  
  114. --socket='/path/to/socket'
  115.   Socket to connect with.
  116.  
  117. --type='table-type'
  118.   Converts tables to the given table type (Default: $opt_type)
  119.   MySQL 3.23 supports at least the BDB, ISAM and MYISAM types.
  120.  
  121. --user='user_name'
  122.   User name to log into the SQL server.
  123.  
  124. --verbose
  125.   This is a test specific option that is only used when debugging a test.
  126.   Print more information about what is going on.
  127.  
  128. --version
  129.   Shows the version of this program.
  130. EOF
  131.   exit(1);
  132. }
  133.